/**  
* @Title: AdminController.java
* @Package com.lemon.back.controller
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年8月1日
* @version V1.0  
*/
package com.lemon.back.controller;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;

import com.lemon.model.cityRun.CrShopUser;
import com.lemon.service.cityRun.CrShopUserService;
import com.lemon.utils.ReqResult;
import com.lemon.utils.StringUtil;
import com.lemon.utils.TokenUtils;

import io.jsonwebtoken.Claims;

/**
 * <p>Title: AdminController</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年8月1日
 * @版本 V 1.0 
 */
@RestController
@RequestMapping(value = "adm")
public class AdminController {

	@Resource
	private CrShopUserService userService;

	public CrShopUser getCrShopUser(HttpServletRequest request) {

		return (CrShopUser) request.getSession().getAttribute("user");
	}

	@RequestMapping("/index.html")
	public ModelAndView index(HttpServletRequest request , HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		CrShopUser tku = getCrShopUser(request);
		String path = "../public/back/admin/index.html";
		if (tku == null) {
			String token = "";
			//通过请求直接获取request中包含的所有的cookie
			Cookie[] cookies = request.getCookies();
			//遍历所有的cookie,然后根据cookie的key值来获取value值
			if (cookies != null) {
				for (Cookie cookie : cookies) {
					if (cookie.getName().equals("_token_")) {
						token = cookie.getValue();
					}
				}
			}
			if (StringUtil.isEmpty(token)) {
				path = "../public/back/admin/login.html";
			} else {
				Claims claims = TokenUtils.parseJWT(token);
				if (claims == null) {
					path = "../public/back/admin/login.html";
				} else {
					String idInfo = claims.getId();
					try {
						CrShopUser userInfo = userService.selectShopUserByAcc(idInfo);
						tku = userInfo;
						request.getSession().setAttribute("user" , userInfo);
					} catch (Exception e) {
						path = "../public/back/admin/login.html";
					}
				}
			}
		}
		modelAndView.addObject("u" , tku);
		modelAndView.setViewName(path);
		return modelAndView;
	}

	@RequestMapping("/welcome.html")
	public ModelAndView welcome(HttpServletRequest request , HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		String path = "../public/back/admin/welcome.html";
		modelAndView.addObject("u" , getCrShopUser(request));
		modelAndView.setViewName(path);
		return modelAndView;
	}

	@RequestMapping("/member-list.html")
	public ModelAndView memberlist(HttpServletRequest request , HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		String path = "../public/back/admin/member-list.html";
		modelAndView.setViewName(path);
		return modelAndView;
	}

	@RequestMapping("/member-add.html")
	public ModelAndView memberadd(HttpServletRequest request , HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		String path = "../public/back/admin/member-add.html";
		modelAndView.setViewName(path);
		return modelAndView;
	}

	@RequestMapping("/order-list.html")
	public ModelAndView orderlist(HttpServletRequest request , HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		String path = "../public/back/admin/order-list.html";
		modelAndView.setViewName(path);
		return modelAndView;
	}

	@RequestMapping("/order-view.html")
	public ModelAndView orderview(HttpServletRequest request , HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		String path = "../public/back/admin/order-view.html";
		modelAndView.setViewName(path);
		return modelAndView;
	}

	@RequestMapping("/order-add.html")
	public ModelAndView orderadd(HttpServletRequest request , HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		String path = "../public/back/admin/order-add.html";
		modelAndView.setViewName(path);
		return modelAndView;
	}

	@RequestMapping("/cash-list.html")
	public ModelAndView cashlist(HttpServletRequest request , HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		String path = "../public/back/admin/cash-list.html";
		modelAndView.setViewName(path);
		return modelAndView;
	}

	@RequestMapping("/login.html")
	public ModelAndView login(HttpServletRequest request , HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		String path = "../public/back/admin/login.html";
		modelAndView.setViewName(path);
		return modelAndView;
	}

	@RequestMapping("/unicode.html")
	public ModelAndView unicode(HttpServletRequest request , HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		String path = "../public/back/admin/unicode.html";
		modelAndView.setViewName(path);
		return modelAndView;
	}

	@RequestMapping("/admin-role.html")
	public ModelAndView adminrole(HttpServletRequest request , HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		String path = "../public/back/admin/admin-role.html";
		modelAndView.setViewName(path);
		return modelAndView;
	}

	@RequestMapping("/admin-cate.html")
	public ModelAndView admincate(HttpServletRequest request , HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		String path = "../public/back/admin/admin-cate.html";
		modelAndView.setViewName(path);
		return modelAndView;
	}

	@RequestMapping("/admin-rule.html")
	public ModelAndView adminrule(HttpServletRequest request , HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		String path = "../public/back/admin/admin-rule.html";
		modelAndView.setViewName(path);
		return modelAndView;
	}

	@RequestMapping("/admin-list.html")
	public ModelAndView adminlist(HttpServletRequest request , HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		String path = "../public/back/admin/admin-list.html";
		modelAndView.setViewName(path);
		return modelAndView;
	}

	@RequestMapping("/admin-add.html")
	public ModelAndView adminadd(HttpServletRequest request , HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		String path = "../public/back/admin/admin-add.html";
		modelAndView.setViewName(path);
		return modelAndView;
	}

	@RequestMapping("/shop-user-list.html")
	public ModelAndView shopulist(HttpServletRequest request , HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		String path = "../public/back/admin/shop-user-list.html";
		modelAndView.setViewName(path);
		return modelAndView;
	}

	@RequestMapping("/shop-list.html")
	public ModelAndView shoplist(HttpServletRequest request , HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		String path = "../public/back/admin/shop-list.html";
		modelAndView.setViewName(path);
		return modelAndView;
	}

	@RequestMapping("/shop-add.html")
	public ModelAndView shopadd(HttpServletRequest request , HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		String path = "../public/back/admin/shop-add.html";
		modelAndView.setViewName(path);
		return modelAndView;
	}

	@RequestMapping("/shop-audit.html")
	public ModelAndView shopaudit(HttpServletRequest request , HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		String path = "../public/back/admin/shop-audit.html";
		modelAndView.setViewName(path);
		return modelAndView;
	}

	@RequestMapping("/user-list.html")
	public ModelAndView userlist(HttpServletRequest request , HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		String path = "../public/back/admin/user-list.html";
		modelAndView.setViewName(path);
		return modelAndView;
	}

	@RequestMapping("/{path}.html")
	public ModelAndView city(@PathVariable String path , String p , HttpServletRequest request ,
			HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		String _path = "../public/back/admin/" + path.replace("|" , "/");
		if (StringUtil.isNotBlank(p)) {
			_path = "../public/back/admin/" + p + "/" + path;
		}
		modelAndView.setViewName(_path);
		return modelAndView;
	}

	// 把指定URL后的字符串全部截断当成参数
	// 这么做是为了防止URL中包含中文或者特殊字符（/等）时，匹配不了的问题
	private static String extractPathFromPattern(final HttpServletRequest request) {
		String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
		String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
		return new AntPathMatcher().extractPathWithinPattern(bestMatchPattern , path);
	}

	@RequestMapping("/relogin.do")
	public ReqResult relogin(HttpServletRequest request , HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		reqResult.setReturnResult("not login" , ReqResult.code_login_error);
		return reqResult;
	}
}

